package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.Ad;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.AdMapper;
import com.cyz.user.userService.AdService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.user.vo.AdVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告接口实现类
*/
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad>
    implements AdService {

    @Override
    public R queryFreeAds() {
        //创建筛选条件
        LambdaQueryWrapper<Ad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询类型为免费的广告
        lambdaQueryWrapper.eq(Ad::getType, MyEnums.FREE_AD_TYPE);
        //查询数据
        List<Ad> list = list(lambdaQueryWrapper);
        //进行vo转换
        List<AdVo> adVos = BeanCopyUtil.copyBeanList(list, AdVo.class);
        //操作判断
        if (adVos.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(adVos);
    }

    @Override
    public R queryMoneyAds() {
        //创建筛选条件
        LambdaQueryWrapper<Ad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询类型为付费的广告
        lambdaQueryWrapper.eq(Ad::getType, MyEnums.MONEY_AD_TYPE);
        //查询数据
        List<Ad> list = list(lambdaQueryWrapper);
        //进行vo转换
        List<AdVo> adVos = BeanCopyUtil.copyBeanList(list, AdVo.class);
        //错误判断
        if (adVos.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(adVos);
    }
}




